package com.ittsang.service.impl;

import com.ittsang.dto.R;
import com.ittsang.dto.RoleUserDTO;
import com.ittsang.dto.UserDTO;
import com.ittsang.mapper.PostsMapper;
import com.ittsang.mapper.UserClubPostsMapper;
import com.ittsang.mapper.UserMapper;
import com.ittsang.pojo.Posts;
import com.ittsang.pojo.UserClubPosts;
import com.ittsang.service.UserClubPostsService;
import com.ittsang.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserClubPostsServiceImpl implements UserClubPostsService {

    @Autowired
    private UserClubPostsMapper userClubPostsMapper;

    @Autowired
    private PostsMapper postsMapper;

    @Autowired
    private UserMapper userMapper;

    // 修改职位
    @Override
    @Transactional
    public R insertRoleUser(RoleUserDTO roleUserDTO) {

        // 获取当前操作用户
        UserDTO user = UserHolder.getUser();
        UserClubPosts curentUserClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), user.getClubId());
        // 判断当前用户是否有权限
        if (curentUserClubPosts == null) {
            return R.error("当前用户无权限");
        }
        // 查询是否原有职位
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(roleUserDTO.getUserId()), roleUserDTO.getClubId());

        // 原来有职位
        if (userClubPosts != null) {
            // 如果是改为会长，DepartmentId为0，PostsId为0，与条件
            if (roleUserDTO.getDepartmentId() == 0 && roleUserDTO.getPostsId() == 0) {
                if (curentUserClubPosts.getPostsId() != 0) {
                    return R.error("当前用户无权限");
                }
                userMapper.alterUserPosts(roleUserDTO.getUserId(), roleUserDTO.getDepartmentId());
                boolean i = userClubPostsMapper.alterRoleUser(roleUserDTO);
                return i ? R.ok("添加成功") : R.error("添加失败");
            }

            // 如果是转为普通成员,DepartmentId不为0，PostsId为-1，与条件
            if (roleUserDTO.getDepartmentId() != 0 && roleUserDTO.getPostsId() == -1) {
                userMapper.alterUserPosts(roleUserDTO.getUserId(), roleUserDTO.getDepartmentId());
                boolean i = userClubPostsMapper.deletedRoleUser(roleUserDTO.getUserId(), roleUserDTO.getClubId());
                return i ? R.ok("添加成功") : R.error("添加失败");
            }

            // 修改职位
            userMapper.alterUserPosts(roleUserDTO.getUserId(), roleUserDTO.getDepartmentId());
            boolean i = userClubPostsMapper.alterRoleUser(roleUserDTO);
            return i ? R.ok("添加成功") : R.error("添加失败");
        }

        // 普通成员转部门
        if (roleUserDTO.getDepartmentId() != 0 && roleUserDTO.getPostsId() == -1) {
            userMapper.alterUserPosts(roleUserDTO.getUserId(), roleUserDTO.getDepartmentId());
            return R.ok("转部门成功");
        }

        // 普通成员转会长
        if (roleUserDTO.getDepartmentId() == 0 && roleUserDTO.getPostsId() == 0) {
            if (curentUserClubPosts.getPostsId() != 0) {
                return R.error("当前用户无权限");
            }
            userMapper.alterUserPosts(roleUserDTO.getUserId(), 0);
            boolean i = userClubPostsMapper.insertRoleUser(roleUserDTO);
            return i ? R.ok("添加成功") : R.error("添加失败");
        }

        // 普通成员转部长
        userMapper.alterUserPosts(roleUserDTO.getUserId(), roleUserDTO.getDepartmentId());

        // 添加一个修改用户表中部门 id 的方法
        boolean i = userClubPostsMapper.insertRoleUser(roleUserDTO);
        return i ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R deletedRoleUser(RoleUserDTO roleUserDTO) {
        boolean i = userClubPostsMapper.deletedRoleUser(roleUserDTO.getUserId(), roleUserDTO.getClubId());
        return i ? R.ok("移除成功") : R.error("移除失败");
    }

    @Override
    public R getAllPosts() {
        UserDTO user = UserHolder.getUser();
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), user.getClubId());
        if (userClubPosts == null) {
            return R.error("用户无权限");
        }
        List<Posts> posts = postsMapper.getAllPosts();
        Posts posts1 = new Posts();
        posts1.setId(0);
        posts1.setName("会长");
        posts1.setDetail("负责社团一切事务");
        posts1.setStatus(false);
        posts.add(posts1);

        Posts posts2 = new Posts();
        posts2.setId(-1);
        posts2.setName("普通成员");
        posts2.setDetail("普通成员");
        posts2.setStatus(false);
        posts.add(posts2);
        return R.ok("获取成功").put("posts", posts);
    }
}
